package org.cy.example.mvp

import org.cy.evolution.framework.ui.listener.OnViewRequestListener
import org.cy.evolution.ioc.injector.EvolutionInjection
import org.cy.evolution.ioc.annotation.AutoInject
import org.cy.evolution.ioc.annotation.Component
import org.cy.evolution.net.http.exception.ApiException
import org.cy.example.domain.entity.ExamEntity
import org.cy.example.domain.repository.ExamRepository

@Component
class ExamPresenter : ExamContract.ExamPresenter {

    @AutoInject
    lateinit var examRepository: ExamRepository

    constructor() : super() {
        EvolutionInjection.inject(this)
    }

    override fun getExamData() {
        examRepository.getExamData(object : OnViewRequestListener<List<ExamEntity>> {
            override fun onSendRequest() {
                view.onShowLoading(0)
            }

            override fun onResult(data: List<ExamEntity>?) {
                if (null == data) {
                    view.onShowError(0, ApiException(-1, "data is null"))
                } else {
                    view.onLoadExamDataSuccess(data)
                }
            }

            override fun onError(aex: ApiException) {
                view.onShowError(0, aex)
            }

            override fun onFinished() {
                view.onHideLoading(0)
            }
        })
    }

}